#!/usr/bin/env python
import os
import sys
import argparse
import flowtool
import http_tls
import add_plugin
import setup_environment
import metadata_attributes
import http_tls_40


parser = argparse.ArgumentParser("This program includes all kinds of QA testing scripts.")

parser.add_argument("--flowtool"
                    , help = "Run flowtool against a single pcap file."
                    , metavar = "capreader_path")

parser.add_argument("-f", "--flowtool_folder"
                    , help = "Run flowtool against all pcap files in this directory and sub-directories."
                    , metavar = ("capreader_path", "path_pcap_folder")
                    , nargs = 2)

parser.add_argument("--miss_http_tls"
                    , help = "Look through this file to find fail classified HTTP & TLS hosts & referers. "
                    , metavar = ("path_flowtool_output", "path_output")
                    , nargs = 2)

parser.add_argument("-a", "--miss_http_tls_folder"
                    , help = "Look through folders for '.pkt.log' file to find fail classified HTTP & TLS hosts & referers. "
                    , metavar = ("path_flowtool_output", "path_output")
                    , nargs = 2)

parser.add_argument("-m", "--miss_http_tls_folder_40"
                    , help = "Looks through folders for '.pkt.log' files and finds failed HTTP and TLS classification. NAVL 4.0 only."
                    , metavar = ("path_flowtool_output", "path_output")
                    , nargs = 2)

parser.add_argument("-p", "--add_plugins"
                    , help = "Add plugins to a NAVL build. "
                    , metavar = ("navl_build", "plugins_dir")
                    , nargs = 2)

parser.add_argument("-e", "--setup_environment"
                    , help = "Copy QClassify files. Make qclassify and capreader"
                    , metavar = "build_path")

parser.add_argument("-M", "--metadata_extraction"
                    , help = "extract specific metadata attributes from NAVL 4.0 flowtool log files"
                    , metavar = ("path_flowtool_output", "path_output")
                    , nargs = 2)

parser.add_argument("-t", "--flowtool_threaded"
                    , help = "Run flowtool against all pcap files in this directory and sub-directories."
                    , metavar = ("capreader_path", "path_pcap_folder")
                    , nargs = 2)


args = parser.parse_args()

def main():
    if args.flowtool:
        ft = flowtool.Flowtool()
        ft.run_flowtool(args.flowtool)
        
    elif args.flowtool_folder:
        ft = flowtool.Flowtool(args.flowtool_folder[0])
        ft.run_flowtool_folder(args.flowtool_folder[1])
        
    elif args.miss_http_tls_folder:
        missing_headers = http_tls.HttpTls()
        missing_headers.missed_http_tls_folder(args.miss_http_tls_folder[0], args.miss_http_tls_folder[1])
        
    elif args.add_plugins:
        plugins = add_plugin.Plugins(args.add_plugins[0], args.add_plugins[1])
        plugins.extract_build()
        plugins.extract_plugins()
        
    elif args.setup_environment:
        environment = setup_environment.Environment(args.setup_environment)

    elif args.miss_http_tls_folder_40:
        ht = http_tls_40.HttpTls()
        ht.missed_http_tls_folder(args.miss_http_tls_folder_40[0], args.miss_http_tls_folder_40[1])

    elif args.metadata_extraction:
        metadata = metadata_attributes.MetadataAttributes()
        metadata.metadata_folder(args.metadata_extraction[0], args.metadata_extraction[1])

    elif args.flowtool_threaded:
        ft = flowtool_thread.Flowtool(args.flowtool_threaded[0])
        jobs = ft.run_flowtool_folder(args.flowtool_threaded[1])
        flowtool_thread.main(jobs)

if __name__ == "__main__":
    sys.exit(main())
